//BusClearingModel
//2020-08-05 09:53:32

package models

import (
	"git.900sui.cn/kc/base/common/functions"
	"git.900sui.cn/kc/base/common/models/base"
	"git.900sui.cn/kc/kcgin/orm"
	"git.900sui.cn/kc/rpcinterface/interface/pay"
)

//表结构体
type BusClearingModel struct {
	Model *base.Model
	Field BusClearingModelField
}

//表字段
type BusClearingModelField struct {
	T_table             string `default:"bus_clearing"`
	F_id                string `default:"id"`
	F_bus_id            string `default:"bus_id"`
	F_bill_date         string `default:"bill_date"`
	F_amount            string `default:"amount"`
	F_depository_amount string `default:"depository_amount"`
	F_usable_amount     string `default:"usable_amount"`
	F_status            string `default:"status"`
	F_agent_type        string `default:"agent_type"`
	F_agent_id          string `default:"agent_id"`
	F_create_time       string `default:"create_time"`
}

//初始化
func (b *BusClearingModel) Init(ormer ...orm.Ormer) *BusClearingModel {
	functions.ReflectModel(&b.Field)
	b.Model = base.NewModel(b.Field.T_table, ormer...)
	return b
}

//新增数据
func (b *BusClearingModel) Insert(data map[string]interface{}) int {
	result, _ := b.Model.Data(data).Insert()
	return result
}

// 根据bus_id和billDate查询
func (b *BusClearingModel) FindByBusIdAndBillDate(busId, billDate int) map[string]interface{} {
	result := b.Model.Where(map[string]interface{}{
		b.Field.F_bus_id:    busId,
		b.Field.F_bill_date: billDate,
	}).Find()
	return result
}

// 根据bus_id和billDate更新
func (b *BusClearingModel) UpdateByBusIdAndBillDate(busId, billDate int, data map[string]interface{}) (int, error) {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_bus_id:    busId,
		b.Field.F_bill_date: billDate,
	}).Data(data).Update()
}

// 根据id和status更新
func (b *BusClearingModel) UpdateByIdAndStatus(id, status interface{}, data map[string]interface{}) (int, error) {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_id:     id,
		b.Field.F_status: status,
	}).Data(data).Update()
}

// 根据busId,agentId，agentType，status更新数据
func (b *BusClearingModel) UpdateAgent(busId, agentId, agentType, status interface{}, data map[string]interface{}) (int, error) {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_bus_id:     busId,
		b.Field.F_agent_id:   agentId,
		b.Field.F_agent_type: agentType,
		b.Field.F_status:     status,
	}).Data(data).Update()
}

// 根据状态获取分页数据
func (b *BusClearingModel) Lists(billDate string, start, limit int) []map[string]interface{} {
	return b.Model.Where(map[string]interface{}{
		b.Field.F_status:    pay.CLEAR_STATUS_none,
		b.Field.F_bill_date: []interface{}{"elt", billDate},
	}).Limit(start, limit).OrderBy(b.Field.F_bus_id).Select()
}

//根据id获取数据
func (b *BusClearingModel) GetById( id int ) map[string]interface{} {
	if id <= 0{
		return map[string]interface{}{}
	}
	
	return b.Model.Where([]base.WhereItem{
		{
			Field: b.Field.F_id,
			Value: id,
		},
	}).Find()
}

//获取代付信息
func (b *BusClearingModel) GetByAgentidAndAgenttype( agentId int, agentType int ) map[string]interface{} {
	if agentId <= 0 || agentType <= 0{
		return map[string]interface{}{}
	}
	rs :=  b.Model.Where([]base.WhereItem{
		{Field: b.Field.F_agent_id, Value: agentId},
		{Field: b.Field.F_agent_type, Value: agentType},
	}).Limit(1).Find()
	return rs
}

//获取信息
func (b *BusClearingModel) GetListByAgentIdAndAgentType(agentId int, agentType int)([]map[string]interface{}){
	if agentId <= 0 || agentType <= 0{
		return make([]map[string]interface{},0)
	}
	rs :=  b.Model.Where([]base.WhereItem{
		{Field: b.Field.F_agent_id, Value: agentId},
		{Field: b.Field.F_agent_type, Value: agentType},
	}).Select()
	return rs
}